<?php
/*
 module:		收藏职位记录
 create_time:	2024-03-12 15:48:50
 author:		
 contact:		
*/

namespace app\api\controller;

use app\api\model\Personalfavorites as PersonalfavoritesModel;
use app\api\service\PersonalfavoritesService;
use think\exception\ValidateException;

class Personalfavorites extends Common
{


    /*start*/

    /**
     * @api {get} /Personalfavorites/index 01、首页数据列表
     */
    function index()
    {
        $limit = $this->request->post('limit', 20, 'intval');
        $page = $this->request->post('page', 1, 'intval');
        $where = [];
        $where['personal_uid'] =  $this->request->uid;
        $field = 'a.did,a.jobs_id,b.jobs_name,b.companyname,b.company_id,b.nature_cn,b.sex_cn,b.age,b.topclass,b.category,b.subclass,b.category_cn,b.minwage,b.maxwage,b.trade_cn,b.district_cn,b.education_cn,b.experience_cn,b.tag_cn,b.refreshtime,b.click,b.negotiable';
        $orderby = 'a.did desc';
        $res =PersonalfavoritesModel::alias('a')
            ->join('jobs b','a.jobs_id=b.id')
            ->where(formatWhere($where))->field($field)
            ->order($orderby)->paginate(['list_rows'=>$limit,'page'=>$page])
            ->toArray();
        if (!empty($res["data"])) {
            $list = $res["data"];
            foreach ($list as &$item) {
                $item["refreshtime"] = timeAnalysis2($item["refreshtime"]);
                if (empty($item["tag_cn"]) || $item["tag_cn"] == "请选择") {
                    $item["tag_cn"] = [];
                } else {
                    $item["tag_cn"] = explode(",", $item["tag_cn"]);
                }
            }
            $res["data"] = $list;
        }
        return $this->ajaxReturn($this->successCode, '返回成功', htmlOutList($res));
    }
    /**
     * @api {post} /Personalfavorites/collection 02、收藏
     */
    function collection()
    {
        $postField = 'jobs_id';
        $data = $this->request->only(explode(',', $postField), 'post', null);
        if (empty($data['jobs_id'])) {
            throw new ValidateException('参数错误');
        }
        $uid=$this->request->uid;
        $where['jobs_id'] = $data['jobs_id'];
        $where['personal_uid'] = $uid;
        $info = PersonalfavoritesModel::where($where)->findOrEmpty()->toArray();

        if (empty($info)) {
            $data['personal_uid'] = $uid;
            $data['jobs_name'] = \app\api\model\Jobs::where("id", $data['jobs_id'])->value("jobs_name");
            $data['addtime'] = time();
            $res = PersonalfavoritesModel::insertGetId($data);
        } else {
            $res = PersonalfavoritesModel::where($where)->delete();
        }
        return $this->ajaxReturn($this->successCode, '操作成功',$res);
    }

    /*end*/

}

